package com.lottery.ruok.lottrey.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lottery.ruok.lottrey.R;
import com.lottery.ruok.lottrey.activity.PrizeListActivity;
import com.nex3z.flowlayout.FlowLayout;

import java.util.HashSet;

import butterknife.BindView;
import butterknife.ButterKnife;
import ui.AreaCircle;
import utils.Utils;

/**
 * Created by ASUS on 2017/11/13.
 */

public class DistributionAdapter extends BaseAdapter {
    private Context mContext;
    private FragmentActivity fragmentActivity;
    private HashSet<String> chooseNumbers;
    public DistributionAdapter(Context context, FragmentActivity fragmentActivity, HashSet<String> chooseNumbers) {
        mContext = context;
        this.fragmentActivity = fragmentActivity;
        this.chooseNumbers = chooseNumbers;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder;
        if (view != null) {
            holder = (ViewHolder) view.getTag();
        } else {
            view = LayoutInflater.from(mContext).inflate(R.layout.prize_number_item, viewGroup, false);
            holder = new ViewHolder(view);
            view.setTag(holder);
        }

        String[] temp = ((PrizeListActivity)fragmentActivity).lotteryStatOneResponses.getIssue_list().get(i).getPrize_num().split(",");
        holder.container.removeAllViews();
        if (temp != null) {
            for (String s : temp) {
                AreaCircle areaCircle = new AreaCircle(mContext);

                if (s.equals("0")) {
                    if (chooseNumbers.contains(s)) {
                        areaCircle.setBackground(Color.parseColor("#de625c"));
                    } else {
                        areaCircle.setBackground(Color.parseColor("#F8DFDE"));
                    }
                } else {
                    int num = Integer.parseInt(s);
                    switch (num % 10) {
                        case 0:
                            if (chooseNumbers.contains(s)) {
                                areaCircle.setBackground(Color.parseColor("#a9a5a5"));
                            } else {
                                areaCircle.setBackground(Color.parseColor("#ededed"));
                            }
                            break;
                        case 1:
                            if (chooseNumbers.contains(s)) {
                                areaCircle.setBackground(Color.parseColor("#9076d2"));
                            } else {
                                areaCircle.setBackground(Color.parseColor("#E8E3F6"));
                            }
                            break;
                        case 2:
                            if (chooseNumbers.contains(s)) {
                                areaCircle.setBackground(Color.parseColor("#dba3ca"));
                            } else {
                                areaCircle.setBackground(Color.parseColor("#f7ecf4"));
                            }
                            break;
                        case 3:
                            if (chooseNumbers.contains(s)) {
                                areaCircle.setBackground(Color.parseColor("#8398cf"));
                            } else {
                                areaCircle.setBackground(Color.parseColor("#e6eaf5"));
                            }
                            break;
                        case 4:
                            if (chooseNumbers.contains(s)) {
                                areaCircle.setBackground(Color.parseColor("#7796ef"));
                            } else {
                                areaCircle.setBackground(Color.parseColor("#E3EAFB"));
                            }
                            break;
                        case 5:
                            if (chooseNumbers.contains(s)) {
                                areaCircle.setBackground(Color.parseColor("#83b6c3"));
                            } else {
                                areaCircle.setBackground(Color.parseColor("#e6f0f2"));
                            }
                            break;
                        case 6:
                            if (chooseNumbers.contains(s)) {
                                areaCircle.setBackground(Color.parseColor("#afcda5"));
                            } else {
                                areaCircle.setBackground(Color.parseColor("#eef4ed"));
                            }
                            break;
                        case 7:
                            if (chooseNumbers.contains(s)) {
                                areaCircle.setBackground(Color.parseColor("#e0cd81"));
                            } else {
                                areaCircle.setBackground(Color.parseColor("#f8f5e4"));
                            }
                            break;
                        case 8:
                            if (chooseNumbers.contains(s)) {
                                areaCircle.setBackground(Color.parseColor("#ec9968"));
                            } else {
                                areaCircle.setBackground(Color.parseColor("#fbeadf"));
                            }
                            break;
                        case 9:
                            if (chooseNumbers.contains(s)) {
                                areaCircle.setBackground(Color.parseColor("#ec6e3c"));
                            } else {
                                areaCircle.setBackground(Color.parseColor("#fce2d7"));
                            }
                            break;
                    }
                }

                areaCircle.setText(s);
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(Utils.dip2px(mContext, 20), Utils.dip2px(mContext, 20));
                areaCircle.setLayoutParams(layoutParams);
                holder.container.addView(areaCircle);
            }
        }

        holder.timeNo.setText((((PrizeListActivity)fragmentActivity).lotteryStatOneResponses.getIssue_list().get(i).getOpen_time().endsWith(":00  ") ? ((PrizeListActivity)fragmentActivity).lotteryStatOneResponses.getIssue_list().get(i).getOpen_time().substring(0, ((PrizeListActivity)fragmentActivity).lotteryStatOneResponses.getIssue_list().get(i).getOpen_time().length() - 5) + "  " : ((PrizeListActivity)fragmentActivity).lotteryStatOneResponses.getIssue_list().get(i).getOpen_time())  + ((PrizeListActivity)fragmentActivity).lotteryStatOneResponses.getIssue_list().get(i).getIssue_no());
        return view;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public int getCount() {
        if (((PrizeListActivity)fragmentActivity).lotteryStatOneResponses != null) {
            return ((PrizeListActivity)fragmentActivity).lotteryStatOneResponses.getIssue_list().size();
        }
        return 0;
    }

    static class ViewHolder {
        @BindView(R.id.time_no)
        TextView timeNo;
        @BindView(R.id.container)
        FlowLayout container;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
